{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_path = \"../models\"\n",
    "image_path = r\"F:\\projects\\ImageAI\\data-images\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sports car  :  65.5918\n",
      "convertible  :  12.3927\n",
      "car wheel  :  8.0013\n",
      "beach wagon  :  5.3135\n",
      "grille  :  4.7835\n"
     ]
    }
   ],
   "source": [
    "from imageai.Classification import ImageClassification\n",
    "import os\n",
    "\n",
    "execution_path = os.getcwd()\n",
    "\n",
    "prediction = ImageClassification()\n",
    "prediction.setModelTypeAsResNet50()\n",
    "prediction.setModelPath(os.path.join(execution_path, f\"{model_path}/resnet50-19c8e357.pth\"))\n",
    "prediction.loadModel()\n",
    "\n",
    "predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, f\"{image_path}/1.jpg\"), result_count=5 )\n",
    "for eachPrediction, eachProbability in zip(predictions, probabilities):\n",
    "    print(eachPrediction , \" : \" , eachProbability)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "person  :  99.99  :  [157, 135, 246, 387]\n",
      "--------------------------------\n",
      "person  :  100.0  :  [540, 103, 577, 224]\n",
      "--------------------------------\n",
      "person  :  99.88  :  [601, 131, 637, 215]\n",
      "--------------------------------\n",
      "person  :  99.37  :  [10, 104, 64, 245]\n",
      "--------------------------------\n",
      "person  :  99.82  :  [458, 143, 511, 266]\n",
      "--------------------------------\n",
      "car  :  99.3  :  [197, 140, 367, 286]\n",
      "--------------------------------\n",
      "motorbike  :  99.01  :  [264, 191, 344, 304]\n",
      "--------------------------------\n",
      "dog  :  94.7  :  [396, 313, 447, 433]\n",
      "--------------------------------\n"
     ]
    }
   ],
   "source": [
    "from imageai.Detection import ObjectDetection\n",
    "import os\n",
    "\n",
    "execution_path = os.getcwd()\n",
    "\n",
    "detector = ObjectDetection()\n",
    "\n",
    "detector.setModelTypeAsYOLOv3()\n",
    "detector.setModelPath(os.path.join(execution_path, f\"{model_path}/yolov3.pt\"))\n",
    "\n",
    "# detector.setModelTypeAsRetinaNet()\n",
    "# detector.setModelPath( os.path.join(execution_path , f\"{model_path}/retinanet_resnet50_fpn_coco-eeacb38b.pth\"))\n",
    "\n",
    "# detector.setModelTypeAsTinyYOLOv3()\n",
    "# detector.setModelPath(os.path.join(execution_path, f\"{model_path}/tiny-yolov3.pt\"))\n",
    "\n",
    "detector.loadModel()\n",
    "\n",
    "# detections = detector.detectObjectsFromImage(\n",
    "#     input_image=os.path.join(execution_path, f\"{image_path}/image2.jpg\"),\n",
    "#     output_image_path=os.path.join(execution_path, \"image2new.jpg\"),\n",
    "#     minimum_percentage_probability=30,\n",
    "# )\n",
    "\n",
    "# detections, objects_path = detector.detectObjectsFromImage(\n",
    "#     input_image=os.path.join(execution_path, f\"{image_path}/image3.jpg\"),\n",
    "#     output_image_path=os.path.join(execution_path, \"image3new.jpg\"),\n",
    "#     minimum_percentage_probability=30,\n",
    "#     extract_detected_objects=True,\n",
    "# )\n",
    "\n",
    "detections = detector.detectObjectsFromImage(\n",
    "    input_image=os.path.join(execution_path, f\"{image_path}/image3.jpg\"),\n",
    "    output_image_path=os.path.join(execution_path, \"image3new_nodetails.jpg\"),\n",
    "    minimum_percentage_probability=30,\n",
    "    display_percentage_probability=False,\n",
    "    display_object_name=False,\n",
    ")\n",
    "\n",
    "\n",
    "for eachObject in detections:\n",
    "    print(\n",
    "        eachObject[\"name\"],\n",
    "        \" : \",\n",
    "        eachObject[\"percentage_probability\"],\n",
    "        \" : \",\n",
    "        eachObject[\"box_points\"],\n",
    "    )\n",
    "    print(\"--------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": " object 'motorcycle' doesn't exist in the supported object classes",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 11\u001b[0m\n\u001b[0;32m      8\u001b[0m detector\u001b[38;5;241m.\u001b[39msetModelPath(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(execution_path, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodel_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/yolov3.pt\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m      9\u001b[0m detector\u001b[38;5;241m.\u001b[39mloadModel()\n\u001b[1;32m---> 11\u001b[0m custom_objects \u001b[38;5;241m=\u001b[39m \u001b[43mdetector\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCustomObjects\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmotorcycle\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[0;32m     12\u001b[0m detections \u001b[38;5;241m=\u001b[39m detector\u001b[38;5;241m.\u001b[39mdetectCustomObjectsFromImage(\n\u001b[0;32m     13\u001b[0m     custom_objects\u001b[38;5;241m=\u001b[39mcustom_objects,\n\u001b[0;32m     14\u001b[0m     input_image\u001b[38;5;241m=\u001b[39mos\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(execution_path, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mimage_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/image3.jpg\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m     15\u001b[0m     output_image_path\u001b[38;5;241m=\u001b[39mos\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(execution_path, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimage3custom.jpg\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m     16\u001b[0m     minimum_percentage_probability\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m30\u001b[39m,\n\u001b[0;32m     17\u001b[0m )\n\u001b[0;32m     19\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m eachObject \u001b[38;5;129;01min\u001b[39;00m detections:\n",
      "File \u001b[1;32mf:\\projects\\image-ai-learning\\.venv\\Lib\\site-packages\\imageai\\Detection\\__init__.py:288\u001b[0m, in \u001b[0;36mObjectDetection.CustomObjects\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m    286\u001b[0m         all_objects_dict[karg] \u001b[38;5;241m=\u001b[39m kwargs[karg]\n\u001b[0;32m    287\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 288\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m object \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkarg\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m doesn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt exist in the supported object classes\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    290\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m all_objects_dict\n",
      "\u001b[1;31mValueError\u001b[0m:  object 'motorcycle' doesn't exist in the supported object classes"
     ]
    }
   ],
   "source": [
    "from imageai.Detection import ObjectDetection\n",
    "import os\n",
    "\n",
    "execution_path = os.getcwd()\n",
    "\n",
    "detector = ObjectDetection()\n",
    "detector.setModelTypeAsYOLOv3()\n",
    "detector.setModelPath(os.path.join(execution_path, f\"{model_path}/yolov3.pt\"))\n",
    "detector.loadModel()\n",
    "\n",
    "custom_objects = detector.CustomObjects(car=True, motorcycle=True)\n",
    "\n",
    "detections = detector.detectCustomObjectsFromImage(\n",
    "    custom_objects=custom_objects,\n",
    "    input_image=os.path.join(execution_path, f\"{image_path}/image3.jpg\"),\n",
    "    output_image_path=os.path.join(execution_path, \"image3custom.jpg\"),\n",
    "    minimum_percentage_probability=30,\n",
    ")\n",
    "\n",
    "for eachObject in detections:\n",
    "    print(\n",
    "        eachObject[\"name\"],\n",
    "        \" : \",\n",
    "        eachObject[\"percentage_probability\"],\n",
    "        \" : \",\n",
    "        eachObject[\"box_points\"],\n",
    "    )\n",
    "    print(\"--------------------------------\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
